﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmChiTietHangSX : Form
    {
        public String action;
        HangSX cNT = new HangSX();
        public frmChiTietHangSX()
        {
            InitializeComponent();
            
        }

        private void frmChiTietNhomThuoc_Load(object sender, EventArgs e)
        {
            if (action == "VIEW" || action == "EDIT") 
            {
                if (cNT.readThongTinHangSX(Session.IDHangSX) == false)
                    MessageBox.Show("Không đọc được thông tin Nhóm Thuốc!");
                else
                {
                    
                    cbxQuocGia.DataSource = Session.getDanhSachQuocGia();
                    cbxQuocGia.DisplayMember = "QuocGia";
                    bindData();
                }
            }

        }

        private void rtxDacDiem_TextChanged(object sender, EventArgs e)
        {

        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "EDIT" || this.action == "ADD")
            {
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnHuy.Enabled = true;
                btnLuu.Enabled = true;
                status = true;
            }
            else if (this.action == "VIEW")
            {
                btnHuy.Enabled = false;
                btnLuu.Enabled = false;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                status = false;
            }
            txtTenNhom.Enabled = status;
            rtxDacDiem.Enabled = status;
            rtxGhiChu.Enabled = status;
            cbxQuocGia.Enabled = status;
            if (this.action == "ADD")
                clearForm();
        }

        public void clearForm()
        {
            txtTenNhom.Text = "";
            rtxDacDiem.Text = "";
            rtxGhiChu.Text = "";
            cbxQuocGia.Text = "Việt Nam";
        }

        public void bindData()
        {
            txtMaNhom.Text = cNT.MaHangSX;
            txtTenNhom.Text = cNT.TenHangSX;
            rtxDacDiem.Text = cNT.DiaChi;
            cbxQuocGia.Text = cNT.QuocGia;
            rtxGhiChu.Text = cNT.GhiChu;
        }

        public void setData()
        {
            if (action == "ADD")
                this.cNT.getMaxID();
            else if (action == "EDIT" || action == "VIEW")
                this.cNT.ID = Session.IDHangSX;
            this.cNT.MaHangSX = txtMaNhom.Text;
            this.cNT.TenHangSX = txtTenNhom.Text;
            this.cNT.QuocGia = cbxQuocGia.Text;
            this.cNT.DiaChi = rtxDacDiem.Text;
            this.cNT.GhiChu = rtxGhiChu.Text;
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            setData();
            if(action == "ADD")
                this.cNT.insertHangSX();
            else if(action == "EDIT")
                this.cNT.updateHangSX();
            this.applyAction("VIEW");
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.applyAction("VIEW");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            applyAction("EDIT");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa Hãng SX này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                setData();
                this.cNT.deleteHangSX();
                this.Close();
            }
        }

     

        private void btnSelectNhomCha_Click(object sender, EventArgs e)
        {
          
        }

        private void frmChiTietNhomThuoc_Enter(object sender, EventArgs e)
        {
            if (action == "ADD" || action == "EDIT")
                if(Session.IDNhomCha > 0)
                {
                    NhomThuoc a =  new NhomThuoc();
                    a.readThongTinNhomThuoc(Session.IDNhomThuoc);
                }
            
        }

        private void frmChiTietNhomThuoc_VisibleChanged(object sender, EventArgs e)
        {
            if (action == "ADD" || action == "EDIT")
                if (Session.IDNhomCha > 0)
                {
                    NhomThuoc a = new NhomThuoc();
                    a.readThongTinNhomThuoc(Session.IDNhomThuoc);
                }
        }

        private void frmChiTietNhomThuoc_Validating(object sender, CancelEventArgs e)
        {
            if (action == "ADD" || action == "EDIT")
                if (Session.IDNhomCha > 0)
                {
                    NhomThuoc a = new NhomThuoc();
                    a.readThongTinNhomThuoc(Session.IDNhomThuoc);
                }
        }

        private void cbxNhomCha_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void setNhomCha(int ID, String fname)
        {
            //cNT.NhomChaID = ID;
        }
    }
}
